#!/bin/sh
# tlp - run commands depending on power source
#
# Copyright (c) 2025 Thomas Koch <linrunner at gmx.net> and others.
# SPDX-License-Identifier: GPL-2.0-or-later

# --- Source libraries

# shellcheck disable=SC2043
for lib in @TLP_TLIB@/tlp-func-base; do
    # shellcheck disable=SC1090
    . "$lib"
done

# --- MAIN
self=${0##*/}

cmd=$1
if [ -z "$cmd" ]; then
    cecho "Usage: $self command [arg(s)]" 1>&2
    exit 1
fi
if ! cmd_exists "$cmd"; then
    cecho "Error: \"$cmd\" not found." 1>&2
    exit 2
fi
shift

case $self in
    run-on-ac)
        get_sys_power_supply
        # shellcheck disable=SC2154
        case "$_syspwr" in
            "$PS_AC") $cmd "$@" ;;
            "$PS_BAT") ;;
            "$PS_UNKNOWN") cecho "Error: power source unknown." 1>&2 ;;
        esac
        ;;

    run-on-bat)
        get_sys_power_supply
        case "$_syspwr" in
            "$PS_AC") ;;
            "$PS_BAT") $cmd "$@" ;;
            "$PS_UNKNOWN") cecho "Error: power source unknown." 1>&2 ;;
        esac
        ;;

    *)
        cecho "Error: unknown mode $self." 1>&2
        exit 1
        ;;
esac
